package com.shuyu.web.controller.api;

import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.shuyu.common.core.controller.BaseController;
import com.shuyu.common.core.domain.Result;
import com.shuyu.service.PayNotifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/order")
@Api(tags = "订单相关")
public class OrderController extends BaseController {
//    @Resource
//    private WxPayService wxService;
    @Resource
    private PayNotifyService payNotifyService;
    @Resource
    protected WxPayService wxPayService;

    @GetMapping("/createOrder")
    @ApiOperation("创建订单")
    @ApiImplicitParams ({
        @ApiImplicitParam(value = "支付类型 0:微信支付；1：支付宝支付；", name = "type", required = true),
        @ApiImplicitParam(value = "购买商品ID", name = "productId", required = true)
    })
    public Result createOrder(int type, int productId,HttpServletRequest request) throws Exception {
        if(type==0){
            return wx(productId,request);
        }else {
            return alipay(productId);
        }
    }
    private Result<WxPayAppOrderResult> wx(int productId, HttpServletRequest request) throws WxPayException {
        WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = payNotifyService.createWxOrder(productId,request);
        WxPayAppOrderResult order = wxPayService.createOrder(wxPayUnifiedOrderRequest);
        return Result.success(order);
    }
    public Result<AlipayTradeAppPayResponse> alipay(int productId) throws Exception {
        AlipayTradeAppPayResponse response = payNotifyService.createAlipayOrder(productId);
        return Result.success(response);
    }

    @GetMapping("/point")
    @ApiOperation("积分支付")
    @ApiImplicitParams ({
            @ApiImplicitParam(value = "购买商品ID", name = "productId", required = true),
            @ApiImplicitParam(value = "活动产品ID", name = "activityProductId", required = true)
    })
    public Result pointPay(int productId,int activityProductId) throws Exception {
        payNotifyService.pointPay(productId,activityProductId);
        return Result.success();
    }

}
